{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1e199d3a-70ea-4f01-a662-41ef98b46608",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\r\n",
    "\n",
    " #5 4PS2操纵杆实验验\n",
    "\n",
    "@----湖南创乐博智能科技有限公司---<br>\n",
    "@文件名件154PS2操纵杆实验验.ipynb <br>\n",
    "@ 版本：V2.0 <br>\n",
    "@ author: zhulin <br@\n",
    " @ 说说PS2操作杆实验,该实验依赖于ADC模块制! <rb>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "961d8c0f-db75-4bb3-9c32-204c064aca5a",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 13.雨滴探测传感器实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：13.雨滴探测传感器实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：雨滴探测传感器实验\n",
    "模数转换模块实验，通过GPIOZero库自带的MCP3008 10位模数转换库直接控制，检测雨滴探测器的模拟量从而判断雨量强度！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5f58331-df2f-409c-b08b-1401a5100a80",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "83db1301-789c-4820-af1b-3a55d0b03465",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gpiozero import Button, MCP3008\n",
    "from gpiozero.tools import absoluted, scaled\n",
    "from signal import pause\n",
    "from time import sleep"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf81ed90-ea51-4447-b522-85494d089b54",
   "metadata": {},
   "source": [
    "## 2.定义使用通道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "20561d89-e334-41e0-9358-07f9b385ffdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "pot_x = MCP3008(channel=0) # 定义PS2操纵杆对应的X方向管脚\n",
    "pot_y = MCP3008(channel=1) # 定义PS2操纵杆对应的y方向管脚\n",
    "pot_z = MCP3008(channel=2) # 定义PS2操纵杆对应的z方向管脚"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e8dc3b8-858e-4434-8062-f44edb629870",
   "metadata": {},
   "source": [
    "## 3.定义方向函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "660f4219-a6c7-4511-a918-14be8f04e8ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "def MAP(x, in_min, in_max, out_min, out_max):\n",
    "    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min\n",
    "    \n",
    "# 方向判断函数\n",
    "def makerobo_direction():\n",
    "    state = ['home', 'up', 'down', 'left', 'right', 'pressed']  # 方向状态信息\n",
    "    i = 0\n",
    "    x_value = round(pot_x.value * 1024)\n",
    "    y_value = round(pot_y.value * 1024)\n",
    "    z_value = round(pot_z.value * 1024)\n",
    "    x_value = round(MAP(x_value,0,1023,0,255))\n",
    "    y_value = round(MAP(y_value,0,1023,0,255))\n",
    "    z_value = round(MAP(z_value,0,1023,0,255))\n",
    "    '''\n",
    "    print('x_value:{},y_value:{},z_value{}'.format(x_value,y_value,z_value))\n",
    "    '''\n",
    "    if x_value <= 30:\n",
    "        i = 1     # up方向\n",
    "    if x_value >= 225:\n",
    "        i = 2    # down方向\n",
    "    if y_value >= 225:\n",
    "        i = 4    # left 方向\n",
    "    if y_value <= 30:\n",
    "        i = 3    # right 方向\n",
    "    if z_value <= 2 and y_value >= 128:\n",
    "        i = 5    # Button 按下\n",
    "    if x_value - 125 < 15 and x_value - 125 > -15\tand y_value - 125 < 15 and y_value - 125 > -15 and z_value >= 250:\n",
    "        i = 0\n",
    "    \n",
    "    return state[i]   # 返回状态"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "208b4e52-3fe4-4815-b17d-f09f435cc846",
   "metadata": {},
   "source": [
    "## 4.循环函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2624aada-828e-4e8e-9995-d4397180d2f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环函数\n",
    "def makerobo_loop():\n",
    "    makerobo_status = ''    # 状态值赋空值\n",
    "    while True:\n",
    "        makerobo_tmp = makerobo_direction()   # 调用方向判断函数\n",
    "        sleep(0.2)\n",
    "        if makerobo_tmp != None and makerobo_tmp != makerobo_status:  # 判断状态是否发生改变\n",
    "            print (makerobo_tmp) # 打印出方向位\n",
    "            makerobo_status = makerobo_tmp # 把当前状态赋给状态值，以防止同一状态多次打印\n",
    "\n",
    "# 异常处理函数\n",
    "def destroy():\n",
    "\tpass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04b01200-b8f4-4906-9fef-ee5921375822",
   "metadata": {},
   "source": [
    "## 5.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45e18609-d482-4008-9b7f-64ee3599f522",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "home\n",
      "up\n",
      "home\n",
      "down\n",
      "home\n",
      "right\n",
      "pressed\n",
      "home\n",
      "left\n",
      "home\n",
      "right\n",
      "home\n",
      "left\n",
      "home\n",
      "left\n",
      "home\n",
      "pressed\n",
      "home\n",
      "pressed\n",
      "home\n",
      "right\n",
      "home\n",
      "pressed\n",
      "home\n",
      "left\n",
      "home\n",
      "pressed\n",
      "home\n",
      "pressed\n",
      "home\n",
      "pressed\n",
      "home\n",
      "pressed\n",
      "home\n",
      "up\n",
      "home\n",
      "up\n",
      "home\n",
      "down\n",
      "home\n",
      "down\n",
      "home\n",
      "pressed\n",
      "home\n",
      "pressed\n",
      "home\n",
      "left\n",
      "home\n",
      "right\n",
      "home\n",
      "right\n",
      "home\n"
     ]
    }
   ],
   "source": [
    "# 程序入口\n",
    "if __name__ == '__main__':\t\t\n",
    "\ttry:\n",
    "\t\tmakerobo_loop() # 调用循环函数\n",
    "\texcept KeyboardInterrupt:  \t# 当按下Ctrl+C时，将执行destroy()子程序。\n",
    "\t\tdestroy()   # 调用释放函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0bd467f-6369-49a4-9363-a1f2401edf9b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
